package cn.unknowpupil.init.service.system.impl;


import cn.unknowpupil.init.domain.dto.LoginUser;
import cn.unknowpupil.init.domain.system.SysLogRecord;
import cn.unknowpupil.init.mapper.system.SysLogRecordMapper;
import cn.unknowpupil.init.service.system.ISysLogRecordService;
import cn.unknowpupil.init.service.system.ISysUserService;
import cn.unknowpupil.init.utils.SecurityUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;


/**
 * Created at 2020-10-16 by  w10g <br>
 * Initialize LogRecord  ... <br>
 * 操作日志记录 核心业务处理实现类
 *
 * @author unknowpupil
 * @since 0.0.1
 */
@Service
public class SysLogRecordServiceImpl extends ServiceImpl<SysLogRecordMapper, SysLogRecord> implements ISysLogRecordService {


    @Resource
    private ISysUserService userService;

    /**
     * 详情
     *
     * @param id {@code Long}   LogRecord ID
     * @return {@link SysLogRecord}
     */
    @Override
    public SysLogRecord info(Long id) {
        return getById(id);
    }

    /**
     * 删除
     *
     * @param ids {@code Long}   LogRecord ID
     * @return {@link SysLogRecord}
     */
    @Override
    public boolean delete(Long[] ids) {
        return removeByIds(Stream.of(ids).collect(Collectors.toList()));
    }


    /**
     * 分页
     *
     * @param current   {@code int} 页码
     * @param size      {@code int} 笔数
     * @param logRecord {@link SysLogRecord}
     * @return {@code IPage<LogRecord>}
     */
    @Override
    public IPage<SysLogRecord> page(int current, int size, SysLogRecord logRecord) {
        IPage<SysLogRecord> page = new Page<>(current, size);
        LambdaQueryWrapper<SysLogRecord> wrapper = new LambdaQueryWrapper<>();
        LoginUser loginUser = SecurityUtils.getLoginUser();

        if (!SecurityUtils.isSuperAdmin(loginUser.getPid())) {

            if (SecurityUtils.isAdmin(loginUser.getPid())) {
                List<String> sunUser = userService.getAllSunUserByUserId(loginUser.getId());
                sunUser.add(loginUser.getUsername());
                wrapper.in(SysLogRecord::getOperName, sunUser);
            } else {
                wrapper.eq(SysLogRecord::getOperName, loginUser.getUsername());
            }
        }


        if (StringUtils.isNotBlank(logRecord.getTitle())) {
            wrapper.like(SysLogRecord::getTitle, logRecord.getTitle());
        }
        if (logRecord.getBusinessType() != null) {
            wrapper.eq(SysLogRecord::getBusinessType, logRecord.getBusinessType());
        }
        if (StringUtils.isNotBlank(logRecord.getStatus())) {
            wrapper.like(SysLogRecord::getStatus, logRecord.getStatus());
        }
        if (StringUtils.isNotBlank(logRecord.getOperName())) {
            wrapper.eq(SysLogRecord::getOperName, logRecord.getOperName());
        }
        if (logRecord.getBusinessTypes() != null) {
            wrapper.and(wrappers -> {
                        for (Integer type : logRecord.getBusinessTypes()) {
                            wrapper.or().eq(SysLogRecord::getOperatorType, type);
                        }
                    }
            );
        }

        wrapper.apply(StringUtils.isNotBlank(logRecord.getBeginTime()),
                "date_format (oper_time,'%Y-%m-%d') >= date_format('" + logRecord.getBeginTime() + "','%Y-%m-%d')");

        wrapper.apply(StringUtils.isNotBlank(logRecord.getEndTime()),
                "date_format (oper_time,'%Y-%m-%d') <= date_format('" + logRecord.getEndTime() + "','%Y-%m-%d')");

        wrapper.orderByDesc(SysLogRecord::getId);
        return super.page(page, wrapper);
    }

}
